package com.wlstock.funi.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wlstock.funi.R;
import com.wlstock.funi.utils.CaculateUtils;

public class CaculateView extends LinearLayout implements TextWatcher, View.OnClickListener {
	
	private String   key;
	private String   value;
	private String   offset;
	
	private TextView tv_cal_key;
	private EditText ed_cal_value;
	private EditText ed_cal_offset;
	private VIEWMODE mode;
	private TextView tv_cal_result;
	public OnValueClickListener valueClickListener;
	public enum VIEWMODE{
		EDIT_MODE, UNEDITMODE
	}
	
	public CaculateView(Context context, VIEWMODE mode){
		this(context);
		this.mode = mode;
		if (mode == VIEWMODE.UNEDITMODE) {
			ed_cal_value.setEnabled(false);
		}else if (mode == VIEWMODE.EDIT_MODE) {
			ed_cal_value.setEnabled(true);
		}
	}
	
	public void initViewValues(String key, String value, String offset){
		this.tv_cal_key.setText(key);
		this.ed_cal_value.setText(value);
		this.ed_cal_offset.setText(offset);
	}
	
	private CaculateView(Context context){
		super(context);
		View inflater = View.inflate(context, R.layout.layout_caculate_view, this);
		tv_cal_key = (TextView)inflater.findViewById(R.id.tv_cal_key);
		ed_cal_value = (EditText)inflater.findViewById(R.id.ed_cal_value);
		ed_cal_offset = (EditText) inflater.findViewById(R.id.ed_cal_offset);
		tv_cal_result = (TextView) inflater.findViewById(R.id.tv_cal_result);
		
		ed_cal_value.addTextChangedListener(this);
		ed_cal_offset.addTextChangedListener(this);
		
		ed_cal_value.setOnClickListener(this);
		tv_cal_result.setOnClickListener(this);
	}
	
	public CaculateView(Context context, AttributeSet attrs) {
		super(context,attrs);
		
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		tv_cal_result.setText(caculate(ed_cal_value.getText().toString(), ed_cal_offset.getText().toString()));
	}
	
	/**
	 * 计算
	 * @param value
	 * @param offset
	 * @return
	 */
	private String caculate(String value, String offset) {
		try {
			float result = CaculateUtils.caculateFloatValue(Float.valueOf(value), Float.valueOf(offset));
			return String.valueOf(result);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "";
	}

	@Override
	public void onClick(View v) {
		String delieverValue = "0";
		switch (v.getId()) {
		case R.id.tv_cal_result:
			TextView tView = (TextView) v;
			delieverValue = tView.getText().toString();
			break;
		case R.id.ed_cal_value:
			EditText editText = (EditText) v;
			 delieverValue = editText.getText().toString();
			break;
		default:
			break;
		}
		if (valueClickListener != null) {
			valueClickListener.deliverValue(delieverValue);
		}
	}

	public interface OnValueClickListener{
		public void deliverValue(String value);
	}
	
}
